﻿using System;
using System.Data;
using System.Web.UI;
using BLL;
using ET;
using Ext.Net;
using HTML;
using Roles;

namespace ERP.Modules
{
    public partial class Destination : UserControl
    {
        private readonly Destination_BLL clsDestination = new Destination_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetData();
                ClearTextBox();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }

        //-------Store-Datasource-----------------
        protected void StoreDestination_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetData();
        }

        //--------Buttom-Main------------------------
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            WindowsDestination.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grDestination.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                ClearTextBox();
                DataDetail(int.Parse(row.RecordID));
                WindowsDestination.Show();
                sm.UpdateSelection();
            }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grDestination.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                try
                {
                    Destination_BLL.Deletetb_Destination(int.Parse(row.RecordID));
                }
                catch (Exception)
                {
                    clsFormat.Alert_ERROR("Thông Báo",
                        "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                }
            }
            GetData();
        }

        //--------Buttom-Popup------------------------
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtDestination_ID.Text == string.Empty)
            {
                InsertDestination();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateDestination();
                }
            }
        }

        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsDestination.Close();
        }

        //----------Click-------------------
        protected void DestinationDblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int Destination_Id = int.Parse(e.ExtraParams["Destination_ID"]);
            DataDetail(Destination_Id);
            WindowsDestination.Show();
        }

        #region[Method]

        protected void GetData()
        {
            DataTable dtDestination = clsDestination.BindGirdtb_Destination(string.Empty);
            if (dtDestination != null && dtDestination.Rows.Count > 0)
            {
                StoreDestination.DataSource = dtDestination;
                StoreDestination.DataBind();
            }
        }

        protected void DataDetail(int id)
        {
            DataTable dtDestination = clsDestination.BindGirdtb_DestinationDetail(id);
            if (dtDestination != null && dtDestination.Rows.Count > 0)
            {
                txtDestination_ID.Text = dtDestination.Rows[0]["Destination_ID"].ToString();
                txtDestination_Alias.Text = dtDestination.Rows[0]["Destination_Alias"].ToString();
                txtDestination_Name.Text = dtDestination.Rows[0]["Destination_Name"].ToString();
                txtDestination_Note.Text = dtDestination.Rows[0]["Destination_Note"].ToString();
            }
        }

        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsDestination = new Destination_BLL();
            DataTable dtDestination = clsDestination.BindGirdtb_Destination(string.Empty);
            if (dtDestination != null && dtDestination.Rows.Count > 0)
            {
                foreach (DataRow dr in dtDestination.Rows)
                {
                    if (txtDestination_ID.Text != dr["Destination_ID"].ToString())
                    {
                        if (txtDestination_Alias.Text == dr["Destination_Alias"].ToString())
                        {
                            kiemtra = true;
                            clsFormat.Alert_ERROR("Thông Báo", "Mã loại khách hàng đã tồn tại !");
                        }
                        else if (txtDestination_Name.Text == dr["Destination_Name"].ToString())
                        {
                            kiemtra = true;
                            clsFormat.Alert_ERROR("Thông Báo", "Tên loại khách hàng đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }

        protected void InsertDestination()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Destination_ET
                    {
                        Destination_Alias = txtDestination_Alias.Text,
                        Destination_Name = txtDestination_Name.Text,
                        Destination_Note = txtDestination_Note.Text,
                        Destination_Date_Add = DateTime.Now,
                        Destination_Computer_Add = Environment.MachineName
                    };
                    Destination_BLL.Inserttb_Destination(obj);
                    clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        protected void UpdateDestination()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Destination_ET
                    {
                        Destination_ID = int.Parse(txtDestination_ID.Text),
                        Destination_Alias = txtDestination_Alias.Text,
                        Destination_Name = txtDestination_Name.Text,
                        Destination_Note = txtDestination_Note.Text,
                        Destination_Date_Modify = DateTime.Now,
                        Destination_Computer_Modify = Environment.MachineName
                    };
                    Destination_BLL.Updatetb_Destination(obj);
                    WindowsDestination.Close();
                    clsFormat.Alert_INFO("Thông Báo", "Sữa thành công!");
                    GetData();
                }
            }
            catch (Exception ex)
            {
                clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        #endregion

        #region[ClearTextbox]

        protected void ClearTextBox()
        {
            FormPanelDestination.Reset();

            txtDestination_ID.Text = string.Empty;
            txtDestination_Alias.Text = MaTuDong();
            txtDestination_Name.Text = string.Empty;
            txtDestination_Note.Text = string.Empty;
        }

        #endregion

        #region[MaTangTuDong]

        protected string MaTuDong()
        {
            int i = 0;
            DataTable dt = clsDestination.BindGirdtb_Destination(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Destination_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Destination_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Destination_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "MĐ_00" + i;
        }

        #endregion

        #region[Search]

        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtDestination = clsDestination.BindGirdtb_Destination(txtSearch.Text);
            if (dtDestination != null && dtDestination.Rows.Count > 0)
            {
                StoreDestination.DataSource = dtDestination;
                StoreDestination.DataBind();
            }
        }

        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Destination, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelDestination.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Destination, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
        //----------Button-Windows---------------
    }
}